package com.ncr.teradata.bireport;

import java.util.Date;

/**
 * ReportMetaData.java
 * 报表元数据类
 *
 * @author ylpeng
 * @date Mar 21, 2007
 */
 public class ReportMetaData
 {
 	private String name;				//报表名称（英文）
 	private String nameCN;				//报表名称（中文）
 	private String type;				//报表类型（Daily/Month）
 	private String cached;				//是否使用报表缓存
 	private String schedulerTime;		//报表调度的时间（HH:MM:SS/DD-DD HH:MM:SS）
 	private String autoCheck;			//是否自动检查报表数据的及时性并自动刷新数据
 	private String autoCheckDimension;	//自动检查的维度（一般为日期或帐务月）
 	private String sql;					//报表SQL语句（每个字段都需要有具体命名和TITLE属性）
 	
 	private int cachedDate;				//缓存中的报表数据的数据日期
 	
 	private int schedulerStartDate;		//报表调度的开始日期（只对月报表有效）
 	private int schedulerEndDate;		//报表调度的结束日期（只对月报表有效）
 	private int schedulerHours;			//报表调度的"时"元素
 	private int schedulerMinutes;		//报表调度的"分"元素
 	private int schedulerSeconds;		//报表调度的"秒"元素（暂时忽略了该元素）
 	
 	/**
 	 * 构造函数
 	 */
 	public ReportMetaData(){
 		cachedDate = -1;
 	}
 	
 	/**
 	 * 调度报表（需判断在时间上该报表是否符合调度条件）
 	 *
 	 *@param time Date 时间参数
 	 */
 	public void doScheduler(Date time){
 		if (!cached.equals("YES"))
 			return;
 		if (canScheduler(time)){
 			DBQuery query = DBFactory.getDBQuery();
 	 	 	ReportData rd = query.executeToReportData(sql);
 	 		ReportCache.setCachedReportData(name,rd);
 	 		cachedDate = time.getDate();
 		}
 	}
 	
 	/**
 	 * 调度报表
 	 */
 	public void doScheduler(){
 		if (!cached.equals("YES"))
 			return;
 		DBQuery query = DBFactory.getDBQuery();
 	 	ReportData rd = query.executeToReportData(sql);
 		ReportCache.setCachedReportData(name,rd);
 		cachedDate = new Date().getDate();
 	}
 	
 	/**
 	 * 查询报表数据
 	 * （用于没有使用缓存的报表）
 	 * 
 	 * @return ReportData 返回报表数据对象
 	 */
 	public ReportData queryReportData(){
 		DBQuery query = DBFactory.getDBQuery();
 	 	return query.executeToReportData(sql);
 	}
 	
 	/**
	 * 检查在某一个时间是否需要调度该报表
	 *
	 *@param time Date 时间参数
	 *@return boolean 返回在参数时间里是否需要调度报表
	 */
	private boolean canScheduler(Date time){
		if(cachedDate == -1)
			return true;
		int month = time.getMonth() + 1;
		int day = time.getDate();
		int hour = time.getHours();
		int minute = time.getMinutes();
		int second = time.getSeconds();
		
		if(type.equals("DAILY")){
			if(cachedDate != day){
				if(hour > schedulerHours)
					return true;
				if(hour >= schedulerHours && minute >= schedulerMinutes)
					return true;
			}
			else{
				if(autoCheck.equals("YES")){
					//检查缓存中的数据是否为最新的数据
					ReportData reportData = ReportCache.getCachedReportData(name);
					int colIdx = reportData.getColumnIndex(autoCheckDimension);
					int restrictColIdx = reportData.getColumnIndex(InitParam.DIMENSION_ALIAS_INSQL_CITY);
					if (colIdx != -1 && restrictColIdx != -1){
						String yesterday = CommonFunc.getYesterday(CommonFunc.getToday("yyyyMMdd"), "yyyyMMdd");
						for (int i = 0; i < InitParam.ALL_LATN_ID.length; i++){
							String latnDate = (String)reportData.getColumnMaxValue(colIdx,restrictColIdx,InitParam.ALL_LATN_ID[i]);
							if (!latnDate.equals(yesterday))
								return true;
						}
					}
				}
			}
		}
		if(type.equals("MONTH")){
			if(cachedDate != day && day >= schedulerStartDate && day <= schedulerEndDate){
				if(hour > schedulerHours)
					return true;
				if(hour >= schedulerHours && minute >= schedulerMinutes)
					return true;
			}
		}
		return false;
	}
	
	public String getName(){
		return name;
	}
	public void setName(String pName){
		name = pName.toUpperCase();
	}
	
	public String getNameCN(){
		return nameCN;
	}
	public void setNameCN(String pNameCN){
		nameCN = pNameCN;
	}
	
	public String getType(){
		return type;
	}
	public void setType(String pType){
		type = pType.toUpperCase();
	}
	
	public String getCached(){
		return cached;
	}
	public void setCached(String pCached){
		cached = pCached.toUpperCase();
	}
	
	public String getSchedulerTime(){
		return schedulerTime;
	}
	public void setSchedulerTime(String pTime){
		schedulerTime = pTime;
		if(type.equals("DAILY")){
			String[] elements = CommonFunc.division(pTime,":");
			schedulerHours = Integer.parseInt(elements[0]);
			schedulerMinutes = Integer.parseInt(elements[1]);
			schedulerSeconds = Integer.parseInt(elements[2]);
		}
		if(type.equals("MONTH")){
			String[] days = CommonFunc.division(pTime.substring(0,pTime.indexOf(" ")),"-");
			schedulerStartDate = Integer.parseInt(days[0]);
			schedulerEndDate = Integer.parseInt(days[1]);
			String[] elements = CommonFunc.division(pTime.substring(pTime.indexOf(" ") + 1),":");
			schedulerHours = Integer.parseInt(elements[0]);
			schedulerMinutes = Integer.parseInt(elements[1]);
			schedulerSeconds = Integer.parseInt(elements[2]);
		}
	}
	
	public String getAutoCheck(){
		return autoCheck;
	}
	public void setAutoCheck(String pAutoCheck){
		autoCheck = pAutoCheck.toUpperCase();
	}
	
	public String getAutoCheckDimension(){
		return autoCheckDimension;
	}
	public void setAutoCheckDimension(String pDimension){
		autoCheckDimension = pDimension;
	}
	
	public String getSql(){
		return sql;
	}
	public void setSql(String pSql){
		sql = pSql;
	}
	
	public int getCachedDate(){
		return cachedDate;
	}
	public void setCachedDate(int pDate){
		cachedDate = pDate;
	}
 }